﻿using System;
using System.Xml.Serialization;
using System.Web.Script.Serialization;

using Fuse8.DomainFramework.Domain;
using Fuse8.DomainFramework.BusinessService;
using Fuse8.DomainFramework.CacheService;

namespace Fuse8.ContentManagementFramework.Domain
{
    [Serializable]
    public partial class CmsPageControl : CmsTrackingEntityObject
    {
        #region Linked field

        private CmsPage _page;

        private CmsTemplateRegion _templateRegion;

        private CmsModule _module;

        #endregion

        #region Properties

        public virtual int PageId { get; set; }

        public virtual int TemplateRegionId { get; set; }

        public virtual int? ModuleId { get; set; }

        public virtual int PageControlTypeKey { get; set; }

        public virtual string PageControlViewValue { get; set; }
        
        public virtual string PageControlModelValue { get; set; }

        public virtual int? PageControlContentId { get; set; }

        public virtual int PageControlOrder { get; set; }

        public virtual string AdminCaption { get; set; }

        #endregion

        #region Linked properties

        [XmlIgnore]
        [ScriptIgnore]
        public CmsPage Page
        {
            get
            {
                if (_page == null)
                {
                    if (PageId != 0)
                    {
                        var businessFacade = new BusinessServiceFacade<CmsPage, int>();
                        if (businessFacade != null)
                        {
                            _page = businessFacade.FindOne(PageId);
                        }
                    }
                }

                return _page;
            }
            set
            {
                PageId = value != null ? value.Id : 0;

                _page = value;
            }
        }

        [XmlIgnore]
        [ScriptIgnore]
        public CmsTemplateRegion TemplateRegion
        {
            get
            {
                if (_templateRegion == null)
                {
                    if (TemplateRegionId != 0)
                    {
                        var businessFacade = new BusinessServiceFacade<CmsTemplateRegion, int>();
                        if (businessFacade != null)
                        {
                            _templateRegion = businessFacade.FindOne(TemplateRegionId);
                        }
                    }
                }

                return _templateRegion;
            }
            set
            {
                TemplateRegionId = value != null ? value.Id : 0;

                _templateRegion = null;
            }
        }

        [XmlIgnore]
        [ScriptIgnore]
        public CmsModule Module
        {
            get
            {
                if (_module == null)
                {
                    if (ModuleId.HasValue && ModuleId != 0)
                    {
                        var businessFacade = new BusinessServiceFacade<CmsModule, int>();
                        if (businessFacade != null)
                        {
                            _module = businessFacade.FindOne(ModuleId.GetValueOrDefault(0));
                        }
                    }
                }
                return _module;
            }
            set
            {
                ModuleId = value != null ? value.Id : new Nullable<int>();

                _module = null;
            }
        }

        #endregion

        #region Constructor

        public CmsPageControl()
        { }

        #endregion

        #region EntityObject override

        public override string TypeParameter
        {
            get
            {
                return "PageControl";
            }
        }

        #endregion
    }
}
